package org.massdgames.thunder.server.domain.strategy;

import java.util.ArrayList;

import org.massdgames.thunder.server.domain.Card;
import org.massdgames.thunder.server.domain.CardType;
import org.massdgames.thunder.server.domain.Deck;
import org.massdgames.thunder.server.domain.DungeonDeck;
import org.massdgames.thunder.server.domain.FeatureDeck;
import org.massdgames.thunder.server.domain.FetchException;

import com.google.inject.Inject;

public class BasicDungeonFetchStrategy extends BasicStrategy implements DungeonFetchStrategy {

    private static final int DEFAULT_DUNGEON_FETCH_COUNT = 3;

    private FeatureDeck featureDeck;
    
    @Inject
    public BasicDungeonFetchStrategy(FeatureDeck featureDeck) {
        this.setNumberOfCardsToFetch(DEFAULT_DUNGEON_FETCH_COUNT);
        this.featureDeck = featureDeck;
    }

    @Override
    public ArrayList<Card> getCards(Deck deck) throws FetchException {
        ensureParameterValidity(deck);

        ArrayList<Card> fetchedCards = new ArrayList<Card>();
        int dungeonFeaturesSelected = 0;

        while (fetchedCards.size() - dungeonFeaturesSelected < numberOfCardsToFetch) {
            Card fetchedCard = deck.getRandomCard();
            if (fetchedCard.getCardType() == CardType.DUNGEON_FEATURE_SELECTOR) {
                ArrayList<Card> featureCards = featureDeck.getCards();
                
                fetchedCards.addAll(featureCards);
                
                dungeonFeaturesSelected += featureCards.size();
            } else {
                fetchedCards.add(fetchedCard);
            }
        }

        return fetchedCards;
    }
}
